package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import lombok.Data;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.Formatter;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description: 新浪科技
 * @author: gaoX
 * @create: 2021-07-28 10:45
 **/
@TargetUrl(value = "https?://(finance.)?sina.com.cn*?202*?html",sourceRegion = "//div[@class='tech-news']")
@Data
public class SinaNews implements AfterExtractor {
	@ExtractBy(value = "//h1/text()", notNull = true)
	private String title;

	@ExtractBy(value = "//div[@class='article']//p/text()", notNull = true)
	private List<String> content;

	@ExtractBy("//div[@class='img_wrapper']//img/@src")
	private List<String> imgList;

	@Formatter(formatter = DataTemplateFormatter.class)
	@ExtractBy("//span[@class='date']/text()")
	private Date date;

	@ExtractBy("//div[@class='channel-path']//a/text()")
	private String category;

	@Override
	public void afterProcess(Page page) {
		ISpiderNewsService service = SpringUtils.getBean(ISpiderNewsService.class);
		SpiderNews spider = new SpiderNews();
		spider.setNewsTitle(ChinaNews.filterSpecialCharacter(title));
		StringBuffer sb = new StringBuffer();
		content.forEach(s -> sb.append(s+"$$$"));
		spider.setNewsContent(ChinaNews.filterSpecialCharacter(sb.toString()));
		spider.setContentSize(spider.getNewsContent().length());
		List<String> newImg = new ArrayList<>();
		for (String s : imgList) {
			if (!s.contains("http")) {
				String url = "https:" + s;
				try {
					String s1 = HttpUtil.get(url);
					newImg.add(url);
				} catch (Exception e) {
					continue;
				}
			} else {
				try {
					String s1 = HttpUtil.get(s);
					newImg.add(s);
				} catch (Exception e) {
					continue;
				}
			}
		}
//		spider.setNewsPicJson(JSONUtil.toJsonStr(newImg));
		spider.setNewsUrl(page.getUrl().toString());
		spider.setNewsPublicTime(date);
		spider.setUpdateBy("admin");
		spider.setCreateTime(DateUtil.date());
		spider.setNewsSource("https://ent.sina.com.cn/");
		if(category.contains("科技")&&page.getUrl().toString().contains("tech")){
			spider.setNewsCategory("新浪科技");
		}else{
			return;
		}

		RedisService redisService = SpringUtils.getBean(RedisService.class);
		if(sb.length()>5000||content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20||newImg.size()>20){
			return;
		}
		Date ago = DateUtil.offsetDay(new Date(),-3);
		int i = date.compareTo(ago);
		//redis查询是否存在
		boolean ismember = redisService.isMember(SecureUtil.md5(title));
		if(!ismember&&i>0){//如果不存在插入mysql和redis
			List<String> resImg = DataSaveUtil.delPic(newImg);
			spider.setNewsPicJson(JSONUtil.toJsonStr(resImg));
			service.save(spider);
			redisService.addTitleSet(SecureUtil.md5(title), spider.getNewsCategory(), date,title);
		}
	}
}
